package com.kaibes.web.admin;

import java.util.Collections;
import java.util.List;

public class AdminMenuUtils {

    public static List<AdminMenu> sort(List<AdminMenu> adminMenuList) {
        if (adminMenuList != null && !adminMenuList.isEmpty()) {
            Collections.sort(adminMenuList);
            for (AdminMenu adminMenu : adminMenuList) {
                if (!adminMenu.getMenuList().isEmpty()) {
                    sort(adminMenu.getMenuList());
                }
            }
        }
        return adminMenuList;
    }

    public static boolean startWith(String str, List<AdminMenu> list) {
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                AdminMenu menu = list.get(i);
                boolean flag = startWith(str, menu);
                if (flag) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean startWith(String str, AdminMenu menu) {
        if (str.endsWith("/") || !menu.getHref().endsWith("/") || !menu.getHref().equals("/")) {
            return str.equals(menu.getHref());
        } else {
            return str.startsWith(menu.getHref());
        }
    }

    public static AdminMenu findMenuByKey(List<AdminMenu> adminMenuList, String key) {
        for (int i = 0; i < adminMenuList.size(); i++) {
            AdminMenu temp = adminMenuList.get(i);
            if (temp.getKey() != null && temp.getKey().equals(key)) {
                return temp;
            }
            if (!temp.getMenuList().isEmpty()) {
                temp = findMenuByKey(temp.getMenuList(), key);
                if (temp != null) {
                    return temp;
                }
            }
        }
        return null;
    }

    public static AdminMenu findByHref(List<AdminMenu> adminMenuList, String uri) {
        // 浅搜索
        for (AdminMenu adminMenu : adminMenuList) {
            if (adminMenu.getHref().equals(uri)) {
                return adminMenu;
            }
            if (!adminMenu.getMenuList().isEmpty()) {
                adminMenu = findByHref(adminMenu.getMenuList(), uri);
                if (adminMenu != null) {
                    return adminMenu;
                }
            }
        }
        return null;
    }

    public static boolean removeByHref(List<AdminMenu> adminMenuList, String uri) {
        AdminMenu target = null;
        // 浅搜索
        for (AdminMenu adminMenu : adminMenuList) {
            if (adminMenu.getHref().equals(uri)) {
                target = adminMenu;
                break;
            }
            if (!adminMenu.getMenuList().isEmpty()) {
                boolean flag = removeByHref(adminMenu.getMenuList(), uri);
                if (flag) {
                    return true;
                }
            }
        }
        return adminMenuList.remove(target);
    }

    public static boolean hasChild(AdminMenu parent, AdminMenu current) {
        if (current == null) {
            return false;
        }
        return current.getParentKey().equals(parent.getKey());
    }

    public static boolean equals(AdminMenu menu1, AdminMenu menu2) {
        if (menu1 == null || menu2 == null) {
            return false;
        }
        return menu1.getKey().equals(menu2.getKey());
    }

}
